package com.sjksy.demo.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sjksy.demo.domain.CustomerInformation;
import com.sjksy.demo.domain.Ledger;
import com.sjksy.demo.domain.TravelContract;
import com.sjksy.demo.domain.TravelMessage;
import com.sjksy.demo.mapper.*;
import com.sjksy.demo.msmapper.Customer.CustomerMsMapper;
import com.sjksy.demo.service.CustomerInformationService;
import com.sjksy.demo.vo.customerInformation.CustomerResponseVo;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;

import java.time.LocalDateTime;
import java.util.List;

/**
* @author guyueyuan
* @description 针对表【customer_information】的数据库操作Service实现
* @createDate 2024-12-11 15:37:02
*/
@Service
public class CustomerInformationServiceImpl extends ServiceImpl<CustomerInformationMapper, CustomerInformation>
    implements CustomerInformationService{
    @Resource
    private CustomerInformationMapper customerInformationMapper;
    @Resource
    private CustomerMsMapper customerMsMapper;
    @Resource
    private TravelMessageMapper travelMessageMapper;
    @Resource
    private LedgerMapper ledgerMapper;
    @Resource
    private TravelContractMapper travelContractMapper;
    @Resource
    private GuideMapper guideMapper;

    /**
     * 插入顾客信息
     * @param customerInformation
     */
    @Transactional
    public void insert(CustomerInformation customerInformation) {
        //插入顾客
        customerInformationMapper.insert(customerInformation);
        //插入分类账信息
        Ledger ledger=new Ledger();
        ledger.setTime(LocalDateTime.now());
        ledger.setCustomerId(customerInformation.getCustomerId());

        TravelMessage travelMessage = travelMessageMapper.selectById(customerInformation.getTravelId());
        TravelContract travelContract = travelContractMapper.selectById(travelMessage.getTravelContractId());

        Integer guideId=travelContract.getGuideId();
        ledger.setRouteId(travelContract.getRouteId());
        ledger.setGuideId(guideId);

        Integer branchId= guideMapper.getBranchName(guideId);

        ledger.setBranchId(branchId);

        ledgerMapper.insert(ledger);

    }

    /**
     * 查询所有顾客信息
     * @return
     */
    public List<CustomerResponseVo> getAll() {
        List<CustomerInformation> customerInformation=customerInformationMapper.selectList(null);
        List<CustomerResponseVo> list=customerMsMapper.EntityToCusVO(customerInformation);

        list.forEach(vo->{
            String name=travelMessageMapper.getRouteName(vo.getTravelId());
            vo.setRouteName(name);
        });
        return list;
    }
}




